package ch.gislb2.math;

public class Calculator {
	
	private double d = 0.7;
	
	public double calulateDistance(double alpha1,double alpha2, double aov, double armlength)
	{
		this.d = armlength;
		double resultA = 0;
		double resultB = 0;
		if(alpha1 >0) 
		{ 
			System.out.println("Berechnung--------------------------------------");
			System.out.println("d: "+ d);
			double f = Math.sin(Math.toRadians(alpha1)) * d;
			System.out.println("f: "+f);
			double beta = Math.abs(90 - alpha1);
			System.out.println("beta: "+beta);
			double y = Math.abs(90-beta);
			System.out.println("y: "+y);
			double h = Math.sin(Math.toRadians(beta))*d;
			System.out.println("h: "+h);
			double delta = Math.abs((aov/2)-y);
			System.out.println("delta: "+delta);
			double g = (Math.cos(Math.toRadians(delta))/Math.sin(Math.toRadians(delta)))*f;
			System.out.println("g: "+g);
			
			resultA = g + h;
			
			System.out.println("resultA "+ resultA);
		}
		if(alpha2 > 0 && alpha2 != alpha1)
		{
			double f = Math.sin(Math.toRadians(alpha2)) * d;
			double beta = 90 - alpha2;
			double y = 90-beta;
			double h = Math.sin(Math.toRadians(beta))*d;
			double delta = (aov/2)-y;
			double g = (Math.cos(Math.toRadians(delta))/Math.sin(Math.toRadians(delta)))*f;
			
			resultB = g + h;
			System.out.println("resultB "+ resultB);
		}
		if(resultA>0 && resultB > 0)
			return (double) Math.round(((resultA + resultB) / 2)*1000)/1000;
		else if(resultA > 0 && resultB == 0)
			return (double) Math.round(resultA*1000)/1000;
		else if(resultA == 0 && resultB > 0)
			return (double) Math.round(resultB*1000)/1000;
		else
			return -1;
	}
	
//	public double getRadiant(double angle)
//	{
//		return 1f / (180f/ Float.parseFloat(String.valueOf(Math.PI)))*angle;
//	}
	public float calculateMedian(Float[] arr)
	{
		if(arr.length % 2 == 0)
		{
			//Gerade anzahl
			//-1 da der Index bei 0 beginnt
			int index = (arr.length / 2)-1;
			Float a = arr[index];
			Float b = arr[index+1];
			return (a + b)/2f;
		}
		else
		{
			//Ungerade Anzahl
			return arr[arr.length/2];
		}
	}
	public double calculateAverage(Float[] arr)
	{
		double sum = 0;
		for(int i = 0;i<arr.length;i++)
		{
			sum = sum + arr[i];
		}
		return sum / arr.length;
	}
//	public float calculateDifference(float val1, float val2)
//	{
//		float temp = Math.abs(val1-val2);
//		if(temp>180)
//			return 360-temp;
//		else
//			return temp;
//	}

}
